<?php
class IndexAction extends CommonAction {
    public function index(){
		$this->display();
    }
    
    Public function verify(){
        import('ORG.Util.Image');
        Image::buildImageVerify(4,2,"png",120,50);
    }
    
    public function userlogin() {
    	if ($this->_post("_submit")) {
	    	$user = M("User");
	    	$username = $this->_post('username');
	    	$password = $this->_post('password','md5');
	    	if(session('verify') != md5($_POST['verifycode'])) {
			   $this->assign("tips",alertTips(L('MSG_VERIFY_ERROR')));
			} else if ($username && $password) {
	    		$userdata = $user->where(array("username"=>$username))->find();
	    		if ($userdata) {
	    			if (!$userdata['salt']) {
	    				$userdata['salt'] = substr(md5(time()),5,4);
	    				$userdata['password'] = md5($userdata['password'].$userdata['salt']);
	    				$user->data(array("password"=>$userdata['password'],"salt"=>$userdata['salt']))->where(array("id"=>$userdata['id']))->save();
	    			}
	   				if ($userdata['password'] == md5($password.$userdata['salt'])) {
		    			$usertoken = md5(time().$userdata['id']);
		    			$user->data(array("usertoken"=>$usertoken))->where(array("id"=>$userdata['id']))->save();
		    			$token  = array("userid"=>$userdata['id'],"userkey"=>md5("vsokey_".$usertoken));
						cookie('vusertoken',$token,24*60*60*7);
						echo "<script> self.location ='".U('/index')."';</script>"; 
		    		} else {
		    			$this->assign("tips",alertTips(L("账户名或密码错误")));
		    		}
	    		} else {
	    			$this->assign("tips",alertTips(L("该账户不存在")));
	    		}
	    	} else {
	    		$this->assign("tips",alertTips(L('请输入用户名或密码')));
	    	}
	    }
    	$this->assign("username",$username);
    	$this->display("signin");
    }
    
    public function logout() {
    	cookie('vusertoken',null);
    	$this->assign("jumpUrl",U('Index/index'));
    	$this->success(L("您已经成功退出"));
    }
    
    public function terms() {
    	$this->display(L("PAGES_TERMS"));
    }
    
    public function password() {
    	if ($this->_post("_submit")) {
    		$usermail = $this->_post("mail");
    		if ($usermail) {
    			$data =M("user")->where(array("usermail"=>$usermail))->find();
    			if ($data) {
    				$newpass = substr(md5(time()),5,8);
    				$udata = urlencode(authcode(serialize(array("usermail"=>$usermail,"newpass"=>$newpass,"usertoken"=>$data["usertoken"])),"ENCODE",C("SITE_KEY"),900));
    				$url = "http://".$_SERVER["HTTP_HOST"].U('/password')."?reset=".$udata;
    				$this->assign("newpass",$newpass);
    				$this->assign("purl",$url);
    				$mailtxt = $this->fetch(L("PAGES_PMAIL"));
    				@sendMail($data["username"],$usermail,L("重置密码"),$mailtxt);
    				$this->assign("jumpUrl",U('/index'));
    				echo $mailtxt;
    				$this->success(L("新的密码已经发到您的邮箱"));
    				exit();
    			} else {
    				$this->error(L("该邮箱不存在"));
    				exit();
    			}
    		}
    		
    	//sendMail("随风","smdcn@qq.com","你好","找回密码");
    	} else if ($this->_get("reset")) {
    		$data = unserialize(authcode($_GET['reset'],"DECODE",C("SITE_KEY")));
    		if ($data){
    			$userdata =M("user")->where(array("usermail"=>$data["usermail"],"usertoken"=>$data["usertoken"]))->find();
	    		if ($userdata) {
	    			$newdata = array();
	    			$newdata['salt'] = substr(md5(time()),5,4);
		    		$newdata['password'] = md5(md5($data['newpass']).$newdata['salt']);
		    		$newdata['usertoken'] = md5(time().$userdata['id']);
		    		M('user')->data($newdata)->where(array("id"=>$userdata['id']))->save();
		    		$this->assign("jumpUrl",U('/signin'));
		    		$this->success(L("你的密码已经被重置"));
		    		exit();
	    		} else {
	    			$this->assign("jumpUrl",U('/index'));
	    			$this->error(L("该邮箱不存在"));
	    			exit();
	    		}
    		} else {
    			redirect(U('/index'));
    		}
    		
    	}
    	$this->display("password");
    }
    
    public function userreg() {
    	if ($this->_post("_submit")) {
	    	$user = D("User");
	    	//echo M("country")->getByEn("CN","code");
	    	
	    	if(session('verify') != md5($_POST['verifycode'])) {
			   $tips = alertTips(L('MSG_VERIFY_ERROR'));
			} else if (!$user->create($_POST,4)) {
	    		$tips = alertTips(L($user->getError()));
	    	} else {
	    		$country = M("country")->getFieldByEn($this->_post("country"),"code");
	    		if (!$country) {
	    			$tips = alertTips(L("请选择你的国家"));
	    		} else {
	    			$salt = substr(md5(time()),5,4);
	    			$user->salt		= $salt;
	    			$user->password = md5(md5($user->password).$salt);
	    			$user->country  = $country;
		    		$user->regtime  = time();
		    		//var_dump($user);
		    		if ($user->add()) {
		    			$this->assign("jumpUrl",U('/index'));
		    			$this->success(L("注册成功"));
		    			exit();
		    		} else {
		    			$tips = alertTips(L("处理失败"));
		    		}
	    		}
	    	}
	    }
	    
	    $this->assign("tips",$tips);
    	$this->display("join");
    }
    
}